﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace libVNEditor
{
    
    public class CAction : Form
    {
        //这个动作要执行的命令名
        public string command
        {
            set
            {
                comboBox_command.Text = value;
            }
            get
            {
                return comboBox_command.Text;
            }
        }

        public string condition
        {
            set
            {
                textBox_condition.Text = value;
            }
            get
            {
                return textBox_condition.Text;
            }
        }
        //命令对应的参数列表
        public List<Argument> args = new List<Argument>();
        private Label label3;
        private TextBox textBox_condition;

        //函数集，不序列化
        List<Function> funcs = new List<Function>();
        public override string ToString()
        {
            string str;
            str = "<action command=\"" + command + "\" condition=\"" + condition + "\">\n";
            foreach(Argument arg in args)
            {
                str += arg.ToString();
            }
            str += "</action>\n";
            return str;
        }

        private TableLayoutPanel tableLayoutPanel1;
        private ListView listView_arguments;
        private ColumnHeader columnHeader_id;
        private ColumnHeader columnHeader_name;
        private ColumnHeader columnHeader_value;
        private Label label1;
        private ComboBox comboBox_command;
        private Label label2;
        private TableLayoutPanel tableLayoutPanel4;
        private Button btn_MoveUp;
        private Button btn_DeleteArgument;
        private Button btn_MoveDown;
        private Button btn_CreateArgument;
        public CAction()
        {
            InitializeComponent();
            funcs.Add(new Function("ChangeBackGround", new string[] { "initializer_list", "mode" }));
            funcs.Add(new Function("SetBackgroundVisible", new string[] { "name", "v" }));
            funcs.Add(new Function("RemoveCharacter", new string[] { "group" }));
            funcs.Add(new Function("Fadein", new string[] { "LayerImage" }));
            funcs.Add(new Function("Lock", new string[] { }));
            funcs.Add(new Function("Next", new string[] { }));
            funcs.Add(new Function("HideDialog", new string[] { }));
            funcs.Add(new Function("ShowDialog", new string[] { }));
            funcs.Add(new Function("Fadeout", new string[] { "LayerImage" }));
            funcs.Add(new Function("CrossDissolveSlow", new string[] { "LayerImage", "next" }));
            funcs.Add(new Function("CrossDissolveSlow", new string[] { "LayerImage", "last" }));
            funcs.Add(new Function("StopMusic", new string[] { }));
            funcs.Add(new Function("ShakeBackground", new string[] { "times" }));
            funcs.Add(new Function("StopSound", new string[] { }));
            funcs.Add(new Function("NextAction", new string[] { "t", "f" }));
            funcs.Add(new Function("Wait", new string[] { "t" }));
            funcs.Add(new Function("AddAction", new string[] { "t", "f" }));
            funcs.Add(new Function("PlaySound", new string[] { "name", "times" }));
            funcs.Add(new Function("PlayMusic", new string[] { "name" }));
            funcs.Add(new Function("Jump", new string[] { "name" }));
        }

        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox_command = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.listView_arguments = new System.Windows.Forms.ListView();
            this.columnHeader_id = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader_name = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader_value = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel();
            this.btn_MoveUp = new System.Windows.Forms.Button();
            this.btn_DeleteArgument = new System.Windows.Forms.Button();
            this.btn_MoveDown = new System.Windows.Forms.Button();
            this.btn_CreateArgument = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox_condition = new System.Windows.Forms.TextBox();
            this.tableLayoutPanel1.SuspendLayout();
            this.tableLayoutPanel4.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 85F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.label3, 0, 3);
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.comboBox_command, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.label2, 0, 2);
            this.tableLayoutPanel1.Controls.Add(this.listView_arguments, 1, 2);
            this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel4, 1, 1);
            this.tableLayoutPanel1.Controls.Add(this.textBox_condition, 1, 3);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 4;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(458, 182);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(79, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "命令名";
            // 
            // comboBox_command
            // 
            this.comboBox_command.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_command.FormattingEnabled = true;
            this.comboBox_command.Items.AddRange(new object[] {
            "RemoveCharacter",
            "Fadein",
            "Lock",
            "Next",
            "HideDialog",
            "ShowDialog",
            "Fadeout",
            "CrossDissolve",
            "StopMusic",
            "ShakeBackground",
            "StopSound",
            "NextAction",
            "Wait",
            "AddAction",
            "PlaySound",
            "PlayMusic",
            "Jump",
            "Choose"});
            this.comboBox_command.Location = new System.Drawing.Point(88, 7);
            this.comboBox_command.Name = "comboBox_command";
            this.comboBox_command.Size = new System.Drawing.Size(367, 20);
            this.comboBox_command.TabIndex = 2;
            this.comboBox_command.SelectedIndexChanged += new System.EventHandler(this.OnCommandSelected);
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(3, 102);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(79, 12);
            this.label2.TabIndex = 4;
            this.label2.Text = "参数";
            // 
            // listView_arguments
            // 
            this.listView_arguments.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader_id,
            this.columnHeader_name,
            this.columnHeader_value});
            this.listView_arguments.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView_arguments.FullRowSelect = true;
            this.listView_arguments.GridLines = true;
            this.listView_arguments.HideSelection = false;
            this.listView_arguments.Location = new System.Drawing.Point(88, 73);
            this.listView_arguments.Name = "listView_arguments";
            this.listView_arguments.Size = new System.Drawing.Size(367, 71);
            this.listView_arguments.TabIndex = 3;
            this.listView_arguments.UseCompatibleStateImageBehavior = false;
            this.listView_arguments.View = System.Windows.Forms.View.Details;
            this.listView_arguments.SelectedIndexChanged += new System.EventHandler(this.SelectedIndexChanged);
            this.listView_arguments.DoubleClick += new System.EventHandler(this.OnEdit);
            // 
            // columnHeader_id
            // 
            this.columnHeader_id.Text = "id";
            this.columnHeader_id.Width = 35;
            // 
            // columnHeader_name
            // 
            this.columnHeader_name.Text = "名称";
            this.columnHeader_name.Width = 100;
            // 
            // columnHeader_value
            // 
            this.columnHeader_value.Text = "值";
            this.columnHeader_value.Width = 120;
            // 
            // tableLayoutPanel4
            // 
            this.tableLayoutPanel4.ColumnCount = 5;
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel4.Controls.Add(this.btn_MoveUp, 0, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_DeleteArgument, 0, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_MoveDown, 1, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_CreateArgument, 0, 0);
            this.tableLayoutPanel4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel4.Location = new System.Drawing.Point(88, 38);
            this.tableLayoutPanel4.Name = "tableLayoutPanel4";
            this.tableLayoutPanel4.RowCount = 1;
            this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel4.Size = new System.Drawing.Size(367, 29);
            this.tableLayoutPanel4.TabIndex = 5;
            // 
            // btn_MoveUp
            // 
            this.btn_MoveUp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveUp.Enabled = false;
            this.btn_MoveUp.Location = new System.Drawing.Point(163, 3);
            this.btn_MoveUp.Name = "btn_MoveUp";
            this.btn_MoveUp.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveUp.TabIndex = 3;
            this.btn_MoveUp.Text = "上移参数";
            this.btn_MoveUp.UseVisualStyleBackColor = true;
            this.btn_MoveUp.Click += new System.EventHandler(this.MoveUpArgumentClick);
            // 
            // btn_DeleteArgument
            // 
            this.btn_DeleteArgument.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_DeleteArgument.Enabled = false;
            this.btn_DeleteArgument.Location = new System.Drawing.Point(83, 3);
            this.btn_DeleteArgument.Name = "btn_DeleteArgument";
            this.btn_DeleteArgument.Size = new System.Drawing.Size(74, 23);
            this.btn_DeleteArgument.TabIndex = 2;
            this.btn_DeleteArgument.Text = "删除参数";
            this.btn_DeleteArgument.UseVisualStyleBackColor = true;
            this.btn_DeleteArgument.Click += new System.EventHandler(this.DeleteArgumentClick);
            // 
            // btn_MoveDown
            // 
            this.btn_MoveDown.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveDown.Enabled = false;
            this.btn_MoveDown.Location = new System.Drawing.Point(243, 3);
            this.btn_MoveDown.Name = "btn_MoveDown";
            this.btn_MoveDown.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveDown.TabIndex = 1;
            this.btn_MoveDown.Text = "下移参数";
            this.btn_MoveDown.UseVisualStyleBackColor = true;
            this.btn_MoveDown.Click += new System.EventHandler(this.MoveDownArgumentClick);
            // 
            // btn_CreateArgument
            // 
            this.btn_CreateArgument.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_CreateArgument.Location = new System.Drawing.Point(3, 3);
            this.btn_CreateArgument.Name = "btn_CreateArgument";
            this.btn_CreateArgument.Size = new System.Drawing.Size(74, 23);
            this.btn_CreateArgument.TabIndex = 0;
            this.btn_CreateArgument.Text = "创建参数";
            this.btn_CreateArgument.UseVisualStyleBackColor = true;
            this.btn_CreateArgument.Click += new System.EventHandler(this.CreateArgumentClick);
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 158);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(79, 12);
            this.label3.TabIndex = 6;
            this.label3.Text = "条件";
            // 
            // textBox_condition
            // 
            this.textBox_condition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_condition.Location = new System.Drawing.Point(88, 154);
            this.textBox_condition.Name = "textBox_condition";
            this.textBox_condition.Size = new System.Drawing.Size(367, 21);
            this.textBox_condition.TabIndex = 7;
            // 
            // CAction
            // 
            this.ClientSize = new System.Drawing.Size(458, 182);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "CAction";
            this.Text = "动作";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.cAction_FormClosing);
            this.Load += new System.EventHandler(this.cAction_Load);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.tableLayoutPanel4.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        public void RefreshForm()
        {
            listView_arguments.Items.Clear();
            //listView_arguments.BeginUpdate();
            for (int arg_count = 0; arg_count < args.Count; arg_count++)
            {
                Argument arg = args[arg_count];
                ListViewItem item = new ListViewItem();
                item.Text = (arg_count + 1).ToString();
                item.SubItems.Add(arg.name);
                item.SubItems.Add(arg.value);
                listView_arguments.Items.Add(item);
            }

            comboBox_command.Items.Clear();
            foreach(Function f in funcs)
            {
                string s = f.name + "(";
                s += string.Join(",", f.args);
                s += ")";
                comboBox_command.Items.Add(s);
            }    
            //listView_arguments.EndUpdate();
        }

        private void SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView_arguments.SelectedIndices.Count == 0) return;
            int idx = listView_arguments.SelectedIndices[0];
            if (idx == -1)
            {
                btn_MoveUp.Enabled = false;
                btn_MoveDown.Enabled = false;
                btn_DeleteArgument.Enabled = false;
            }
            else if (idx == 0)
            {
                btn_DeleteArgument.Enabled = true;
                if (idx == listView_arguments.Items.Count - 1)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx < listView_arguments.Items.Count - 1)
                {
                    btn_MoveDown.Enabled = true;
                    btn_MoveUp.Enabled = false;
                }
            }
            else if (idx == listView_arguments.Items.Count - 1)
            {
                btn_DeleteArgument.Enabled = true;
                if (idx == 0)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx > 0)
                {
                    btn_MoveDown.Enabled = false;
                    btn_MoveUp.Enabled = true;
                }
            }
            else
            {
                btn_DeleteArgument.Enabled = true;
                btn_MoveUp.Enabled = true;
                btn_MoveDown.Enabled = true;
            }
        }

        private void CreateArgumentClick(object sender, EventArgs e)
        {
            string name = Interaction.InputBox("请输入参数名称：");
            string val = Interaction.InputBox("请输入初始值：");
            if (name == "") return;
            Argument arg = new Argument
            {
                name = name,
                value = val
            };
            args.Add(arg);
            RefreshForm();
        }

        private void OnEdit(object sender, EventArgs e)
        {
            int idx = listView_arguments.SelectedIndices[0];
            if (idx == -1) return;
            /*
            string name = Interaction.InputBox("请输入新的参数名:", "重命名", args[idx].name);
            if(name == "")
            {
                MessageBox.Show("参数名不能为空");
                return;
            }
            */
            string value = Interaction.InputBox("请输入新的参数值:", "重命名", args[idx].value);
            //args[idx].name = name;
            args[idx].value = value;
            RefreshForm();
        }

        private void DeleteArgumentClick(object sender, EventArgs e)
        {
            int idx = listView_arguments.SelectedIndices[0];
            if (idx == -1) return;
            DialogResult result = MessageBox.Show("确定要删除这个参数吗?", "删除参数", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                args.RemoveAt(idx);
                RefreshForm();
            }
        }

        private void MoveUpArgumentClick(object sender, EventArgs e)
        {
            int idx = listView_arguments.SelectedIndices[0];
            Argument s1 = args[idx];
            Argument s2 = args[idx - 1];
            args[idx] = s2;
            args[idx - 1] = s1;
            RefreshForm();
        }

        private void MoveDownArgumentClick(object sender, EventArgs e)
        {
            int idx = listView_arguments.SelectedIndices[0];
            Argument s1 = args[idx];
            Argument s2 = args[idx + 1];
            args[idx] = s2;
            args[idx + 1] = s1;
            RefreshForm();
        }

        private void cAction_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
        }

        private void cAction_Load(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void OnCommandSelected(object sender, EventArgs e)
        {
            int idx = comboBox_command.SelectedIndex;
            if (idx == -1 || idx >= comboBox_command.Items.Count ) return;
            string[] arguments = funcs[idx].args;
            listView_arguments.Items.Clear();
            args.Clear();
            for (int arg_count = 0; arg_count < arguments.Length; arg_count++)
            {
                string arg = arguments[arg_count];
                ListViewItem item = new ListViewItem { Text = (arg_count + 1).ToString() };
                item.SubItems.Add(arg);
                item.SubItems.Add("");
                listView_arguments.Items.Add(item);
                args.Add(new Argument { name = arg, value = "" });
            }
        }
    }


    public class Function
    {
        public string name;
        public string[] args;
        public Function(string name, string[] args)
        {
            this.name = name;
            this.args = args;
        }
    }

    public class Argument
    {
        public string name;//参数名
        public string value;//值

        public override string ToString()
        {
            string str;
            str = "<argument name=\"" + name + "\" value=\"" + value + "\"></argument>\n";
            return str;
        }
    }
}
